package com.atguigu.rabbitmq.four;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class ConfirmMessage {
    public static final int message_count=1000;
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
//        ConfirmMessage.publishMessageIndividually();//80734ms
//        ConfirmMessage.publishMessageBatch();//986ms
        ConfirmMessage.publishMessageAsync();//208ms

    }
    public static void publishMessageIndividually() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMqUtils.getChannel();
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName,true,false,false ,null);
        channel.confirmSelect();
        long begin = System.currentTimeMillis();

        for (int i = 0; i < message_count; i++) {
            channel.basicPublish("",queueName,null,(i+"").getBytes());
            boolean flag = channel.waitForConfirms();
            if (flag){
                System.out.println("消息发送成功");
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("发布"+message_count+"条单独确认消息,耗时a"+(end-begin)+"ms");
    }
    public static void publishMessageBatch() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMqUtils.getChannel();
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName,true,false,false ,null);
        channel.confirmSelect();
        long begin = System.currentTimeMillis();
        int batchSize=100;
        for (int i = 0; i < message_count; i++) {
            channel.basicPublish("",queueName,null,(i+"").getBytes());
            if (i%batchSize==0){
                boolean flag= channel.waitForConfirms();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("发布"+message_count+"条批量确认消息,耗时"+(end-begin)+"ms");
    }
    public static void publishMessageAsync() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMqUtils.getChannel();
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName,true,false,false ,null);
        channel.confirmSelect();
        long begin = System.currentTimeMillis();
        channel.addConfirmListener((deliveryTag, multiple) -> {
            System.out.println("确认的消息"+deliveryTag);
        },(deliveryTag, multiple) -> {
            System.out.println("未确认的消息"+deliveryTag);
        });
        for (int i = 0; i < message_count; i++) {
            channel.basicPublish("",queueName,null,(i+"").getBytes());
        }
        long end = System.currentTimeMillis();
        System.out.println("发布"+message_count+"条异步消息,耗时"+(end-begin)+"ms");
    }

}
